<template>
	<view class="login">
		<u-navbar title="登录" @leftClick="rightClick" :autoBack="false"></u-navbar>
		<view class="login-in">
			<!--登录start-->
			<view class="login-logo"
				:style="{width: '200rpx',height: '200rpx',borderRadius: '15rpx',background: 'rgba(0,0,0,.3)', marginTop: '100rpx'}">
				<image src="@/static/home/logo.png" mode="aspectFill"></image>
			</view>
			<!--登录end-->
			
			<view>

				<!--  #ifdef MP-ALIPAY  -->
				<!-- 支付宝 -->
				<button @getAuthorize="getAuthCode" @error="AuthorizeError" open-type="getAuthorize" scope='phoneNumber'
					class="logon-phone">
					<text style="color: #FFFFFF">本机号码一键登录</text>
				</button>
				<!-- #endif -->
				<!--  #ifdef MP-WEIXIN  -->
				<button open-type="getPhoneNumber" @getphonenumber="phoneLogin" class="logon-phone" v-if="is_register === 0">
					<text style="color: #FFFFFF">本机号码一键登录</text>
				</button>
				<!-- #endif -->

				<view class="no-login" @click="noLogin">暂不登录</view>
				<!--登录按钮end-->

				<!--隐私协议start-->
				<!-- <view :style="{color: '#999',fontSize: '24rpx',paddingTop: '30rpx'}">
					登录代表您已同意<text :style="{color: 'red'}">《用户服务协议》</text>及<text :style="{color: 'red'}">《隐私政策》</text>
				</view> -->
				<!--隐私协议end-->
				
				
				<!-- 隐私协议 -->
				<!-- <uni-popup ref="popup">
					<view class="popupWrap">
						<view class="popupTxt">
							 在你使用【{{ plat_name }}】 服务之前，请仔细阅读<text class="blueColor" @click="handleOpenPrivacyContract"> 《{{ plat_name }}隐私保护指引》</text>。如你同意《{{ plat_name }}隐私保护指引》，请点击“同意”开始使用【{{ plat_name }}】。
						</view>
						<view class="popupBot">
							<button id="disagree-btn" type="default" @click="handleDisagree">拒绝</button>
							<button id="agree-btn" type="primary" open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button>
						</view>
					</view>
				</uni-popup> -->
				
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/api/mine.js';
	export default {
		data() {
			return {
				out_box_with: 'red',
				form: {
					code: '', //授权码
					userInfo: '', //用户基本信息
					iv: '', //获取手机号必填
					encryptedData: '',
					invite: '' //邀请码
				},
				currentpage: '',
				 is_register:0,
				// needAuthorization:true,
				// plat_name:'加油保小程序'
			}
		},
		
		onLoad(options) {
			this.currentpage = options;
			
			this.is_register = uni.getStorageSync('is_register');//判断用户是否注册
			if(this.is_register==0){
				this.getIsRegister();
			}else{
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/home/index'
					});
				}, 1000);
			}
		
			// #ifndef MP-ALIPAY
			uni.login({
				success: (log) => {
					this.form.code = log.code;
				}
			});
			this.form.invite = uni.getStorageSync('userDistCode');
			// #endif

			// #ifdef MP-ALIPAY
			// 支付宝小程序关闭页面下拉(默认开启下拉)
			my.setCanPullDown({
				canPullDown: false
			});
			// #endif
		},
		methods: {
			getIsRegister(){
				uni.login({
				        provider: "weixin",
				        success: (wxInfo) => {
				          // 获取到code后，提交给服务端
						  mApi.appletLogin( {
				            code: wxInfo.code,
				          }).then(res => {
							    this.is_register = res.data.is_register
								if( this.is_register==1){
									uni.setStorage({ key: 'userinfo',data: {token: res.data.userinfo.token}});
									uni.reLaunch({
										url: "pages/mine/index"
									});
									
								}
						  })
						  
				        },
				});
			},
			// handleDisagree(e) {
			// 					  this.$refs.popup.close();
			// 				  },
			//   handleAgreePrivacyAuthorization(res) {
			// 				  // 用户同意隐私协议事件回调
			// 				  // 用户点击了同意，之后所有已声明过的隐私接口和组件都可以调用了
			// 				  this.$refs.popup.close()
			// 
			// 				},
			//   handleOpenPrivacyContract(){
			// 				// 打开隐私协议页面
			// 				wx.openPrivacyContract({
			// 				  success: () => {}, // 打开成功
			// 				  fail: () => {}, // 打开失败
			// 				  complete: (res) => {
			// 					  console.log(res,"openPrivacyContract complete");
			// 				  }
			// 			})
			// },
			rightClick() {
				console.log("退出");
				uni.switchTab({
					url: '/pages/home/index'
				})
			},
			// toHome(){
			// 	console.log('回首页');
			// },
			// 退出登录
			noLogin() {
				uni.switchTab({
					url: '/pages/home/index'
				})
			},
			// 微信登录
			phoneLogin(e) {
				let _this = this
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					_this.form.iv = e.detail.iv, //获取手机号必填
						_this.form.encryptedData = e.detail.encryptedData
						
						api.login(_this.form).then(res => {
							if (res.code == 1) {
								uni.setStorageSync('userinfo', res.data
									.userinfo)
								uni.reLaunch({
									url: '/pages/home/index'
								})
							} else {
								uni.hideLoading();
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								});
							}
						})
						
				} else {
					uni.showToast({
						title: '您已取消授权!',
						icon: 'none'
						// dur
					})
				}
			},
			// 支付宝小程序  手机号授权登录
			getAuthCode() {
				let that = this;
				my.getPhoneNumber({
					success: (res) => {
						that.form.encryptedData = JSON.parse(res.response).response;
						uni.showModal({
							title: '绑定个人信息',
							content: '仅用于展示头像、昵称信息',
							success: (e) => {
								uni.showLoading({
									title: '加载中…'
								});
								if (!e.cancel) {
									my.getAuthCode({
										scopes: ['auth_user'],
										success: (res) => {
											uni.showLoading({
												title: '登录中…'
											});
											console.log(res);
											api.alipayLogin({
												encryptedData: that.form
													.encryptedData,
												code: res.authCode,
												invite: uni.getStorageSync(
														'userDistCode') ? uni
													.getStorageSync(
														'userDistCode') : ""
											}).then(result => {
												if (result.code == 1) {
													uni.setStorageSync(
														'userinfo', result
														.data
														.userinfo);
													setTimeout(() => {
														uni.reLaunch({
															url: '/pages/home/index'
														});
													}, 1500);
												};
												uni.hideLoading();
												my.showToast({
													type: 'success',
													content: result
														.msg,
													duration: 2000
												});
											}).catch(e => {
												uni.hideLoading();
											});
										},
									});
								} else {
									uni.showToast({
										title: '您已取消操作！',
										icon: 'none',
										duration: 1000,
										success: () => {
											uni.hideLoading();
										}
									});
								}
							},
							fail: err => {}
						});
						// that.$refs.toastModel.open();
					},
					fail: (err) => {
						my.showToast({
							content: err.errorMessage,
							duration: 2000
						});
					},
				});
			},
			// 授权 错误
			AuthorizeError(err) {
				// console.log(err);
			}
		}
	}
</script>

<style>
	@import './source/iconfont/iconfont.css';

	/* button::after {
		border: none;
		background-color: #f6424d;
		color: #FFFFFF!important;
	} */
</style>
<style lang="scss" scoped>
	.login {
		width: 100%;

		.login-in {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 130rpx;

			.login-logo {
				overflow: hidden;
				border-radius: 15rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			//:style="{background: out_box_with,height: '100rpx'}"
		}

		.wechat-btn {
			line-height: 0 !important;
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;
		}
	}

	.logon-phone {
		width: 690rpx;
		height: 90rpx;
		line-height: 90rpx;
		color: #FFFFFF !important;
		background: #f6424d;
		border: none;
		margin-top: 200rpx;
	}

	.no-login {
		width: 690rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 32rpx;
		color: #FFFFFF;
		background:#999999;
		margin-top: 40rpx;
		border: none;
		border-radius: 10rpx;
		//padding-top: 40rpx;
	}
	
	// .popupWrap{
	// 		  width: 540rpx;
	//  	box-sizing: border-box;
	//  	padding:42rpx;
	//  	background:white;
	//  	border-radius: 30rpx;
	// 		z-index: 999999;
	// 		.blueColor{
	// 			color: rgba(39, 152, 240, 1);
	// 		}
	// 		.popupTxt{
	// 			line-height: 48rpx;
	// 		}
	//  	.popupBot{
	//  		display: flex;
	// 			justify-content: space-around;
	// 			align-items: center;
	// 			margin-top: 30rpx;
	//  	}
	//  }
</style>
